package kr.hkit.androidcomplete01.chapter08;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnLongClickListener;

public class MoveCircle extends Activity {
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		final MyView vw = new MyView(this);
		vw.setOnLongClickListener(new OnLongClickListener() {
			public boolean onLongClick(View v) {
				if (vw.mColor == Color.RED){
					vw.mColor = Color.BLUE;
				}else{
					vw.mColor = Color.RED;
				}
				return true;
			}
		});
		setContentView(vw);
	}
	
	class MyView extends View{
		float mX;
		float mY;
		int mColor;
		Paint paint;
		
		public MyView(Context context) {
			super(context);
			mX = 100;
			mY = 100;
			mColor = Color.BLUE;
			paint = new Paint(Paint.ANTI_ALIAS_FLAG);
			paint.setColor(mColor);
		}
		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);
			paint.setColor(mColor);
			canvas.drawCircle(mX, mY, 16, paint);
		}
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			super.onTouchEvent(event);
			if (event.getAction() == MotionEvent.ACTION_DOWN){
				mX = event.getX();
				mY = event.getY();
				invalidate();
				return true;
			}
			return false;
		}
	}
}
